﻿package com.tudou.fast
{
    import com.tudou.track.*;
    import com.tudou.utils.*;
    import flash.events.*;
    import flash.net.*;

    public class FastTudouNew extends EventDispatcher
    {
        private var aop:AOPTracker;
        private var itemId:uint = 0;
        private var failCount:uint = 0;
        private var newLoader:URLLoader;
        private var canReportFast:Boolean = false;
        private var fastVersion:Array;
        private var checkState:String;
        private var itemType:String;
        private var fastVersionStr:String = "0";
        private var fastType:String;
        private var oldSocket:XMLSocket;
        private static const NEW_FAST_TUDOU:String = "new_fast_tudou";
        private static const CHECKED:String = "checked!";
        private static var instance:FastTudouNew = null;
        private static const SHAKE_NEW_PORT:int = 9701;
        private static const CHECKING:String = "checking!";
        private static const UNCHECKED:String = "unCheck!";
        private static const OLD_FAST_TUDOU:String = "old_fast_tudou";
        private static const NEW_HOST:String = "127.0.0.1";
        private static const OLD_HOST:String = "127.0.0.1";
        private static const NO_FAST_TUDOU:String = "no_fast_tudou";
        private static const NEW_PORT:int = 9415;
        private static const S_FING:String = "#!";
        private static const NEW_FAST_SHAKE:String = "http://" + NEW_HOST + ":" + SHAKE_NEW_PORT + "/version";
        private static var lock:Boolean = true;
        private static const S_LING:String = "$CMD";
        private static const CMD_INFO:String = "INFO";
        private static const S_SPTA:String = "|";
        private static const OLD_PORT:int = 9207;

        public function FastTudouNew()
        {
            fastVersion = [0];
            if (!lock)
            {
                checkState = UNCHECKED;
                fastType = NO_FAST_TUDOU;
                aop = AOPTracker.getInst();
            }
            else
            {
                throw new Error("This is a singleton class FastTudouFactory!");
            }
            return;
        }// end function

        private function newConnected(event:Event) : void
        {
            var _loc_4:int = 0;
            fastType = NEW_FAST_TUDOU;
            var _loc_2:* = newLoader.data;
            var _loc_3:* = _loc_2.split(".");
            if (_loc_3.length > 1)
            {
                fastVersion = new Array();
                _loc_4 = 0;
                while (_loc_4 < _loc_3.length)
                {
                    
                    fastVersion[_loc_4] = uint(_loc_3[_loc_4]);
                    _loc_4++;
                }
            }
            else
            {
                fastVersion = [1, 30];
            }
            canReportFast = fastVersion[0] >= 1 && fastVersion[1] >= 40;
            fastVersionStr = fastVersion.join(".");
            killOldFast();
            dispatchComplete();
            aop.addNodeLog(NodeLog.FAST_REQUEST_COMPLETE);
            return;
        }// end function

        private function newFastUrl(param1:String) : String
        {
            var i:int;
            var url:* = param1;
            var reg:* = /^(http:\/\/)(.+?)(flv|mp4|f4v|m4v|mv4)(.+?)(flv|mp4|f4v|m4v|mv4)(\?.+?&key=)(.)(.+)$""^(http:\/\/)(.+?)(flv|mp4|f4v|m4v|mv4)(.+?)(flv|mp4|f4v|m4v|mv4)(\?.+?&key=)(.)(.+)$/i;
            var a:* = reg.exec(url);
            var newUrl:String;
            if (a == null)
            {
                return url;
            }
            try
            {
                newUrl = newUrl + (a[1] + NEW_HOST + ":" + NEW_PORT + "/");
                i;
                while (i < a.length)
                {
                    
                    newUrl = newUrl + a[i];
                    i = (i + 1);
                }
                newUrl = newUrl + "&playtype=1";
            }
            catch (e:Error)
            {
                return url;
            }
            return newUrl;
        }// end function

        private function oldConnected(event:Event) : void
        {
            fastType = OLD_FAST_TUDOU;
            if (itemId != 0)
            {
                sendVideoInfo(itemId, itemType);
            }
            fastVersion = [1, 20];
            fastVersionStr = fastVersion.join(".");
            killNewFast();
            dispatchComplete();
            aop.addNodeLog(NodeLog.FAST_REQUEST_COMPLETE);
            return;
        }// end function

        public function fastUrl(param1:String) : String
        {
            if (fastType == OLD_FAST_TUDOU)
            {
                return param1;
            }
            if (fastType == NEW_FAST_TUDOU)
            {
                param1 = newFastUrl(param1);
                return param1;
            }
            return param1;
        }// end function

        public function get version() : String
        {
            return fastVersionStr;
        }// end function

        private function connectOldFast() : void
        {
            try
            {
                if (oldSocket == null)
                {
                    oldSocket = new XMLSocket();
                    oldSocket.addEventListener(Event.CONNECT, oldConnected);
                    oldSocket.addEventListener(DataEvent.DATA, onDataHandler);
                    oldSocket.addEventListener(IOErrorEvent.IO_ERROR, IOErrorHandler);
                    oldSocket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
                    oldSocket.connect(OLD_HOST, OLD_PORT);
                    checkState = CHECKING;
                }
            }
            catch (e:Error)
            {
            }
            return;
        }// end function

        public function get isChecked() : Boolean
        {
            return checkState == CHECKED;
        }// end function

        private function killNewFast() : void
        {
            if (newLoader != null)
            {
                try
                {
                    newLoader.close();
                    newLoader.removeEventListener(Event.COMPLETE, newConnected);
                    newLoader.removeEventListener(IOErrorEvent.IO_ERROR, IOErrorHandler);
                    newLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
                }
                catch (e:Error)
                {
                }
                newLoader = null;
            }
            return;
        }// end function

        private function connectNewFast() : void
        {
            try
            {
                if (newLoader == null)
                {
                    newLoader = new URLLoader();
                    newLoader.addEventListener(Event.COMPLETE, newConnected);
                    newLoader.addEventListener(IOErrorEvent.IO_ERROR, IOErrorHandler);
                    newLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
                    newLoader.load(new URLRequest(NEW_FAST_SHAKE + "?noCache=" + RandString.noCache));
                    checkState = CHECKING;
                }
            }
            catch (e:Error)
            {
            }
            return;
        }// end function

        private function securityErrorHandler(event:SecurityErrorEvent) : void
        {
            aop.addNodeLog(NodeLog.FAST_REQUEST_ERROR);
            var _loc_3:* = failCount + 1;
            failCount = _loc_3;
            if (failCount == 2)
            {
                dispatchComplete();
            }
            return;
        }// end function

        private function killOldFast() : void
        {
            if (oldSocket != null)
            {
                try
                {
                    oldSocket.close();
                    oldSocket.removeEventListener(Event.COMPLETE, newConnected);
                    oldSocket.removeEventListener(IOErrorEvent.IO_ERROR, IOErrorHandler);
                    oldSocket.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
                    oldSocket.removeEventListener(DataEvent.DATA, onDataHandler);
                }
                catch (e:Error)
                {
                }
                oldSocket = null;
            }
            return;
        }// end function

        private function IOErrorHandler(event:IOErrorEvent) : void
        {
            aop.addNodeLog(NodeLog.FAST_REQUEST_ERROR);
            var _loc_3:* = failCount + 1;
            failCount = _loc_3;
            if (failCount == 2)
            {
                dispatchComplete();
            }
            return;
        }// end function

        private function dispatchComplete() : void
        {
            checkState = CHECKED;
            var _loc_1:* = new FastTudouEvent(FastTudouEvent.CHECK_COMPLETE);
            _loc_1.result = isChecked;
            dispatchEvent(_loc_1);
            return;
        }// end function

        private function onDataHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data.split(S_SPTA);
            if (_loc_2[0] == S_LING && _loc_2[(_loc_2.length - 1)] == S_FING)
            {
            }
            return;
        }// end function

        public function connect() : void
        {
            if (checkState == CHECKING)
            {
                return;
            }
            aop.addNodeLog(NodeLog.FAST_REQUEST_START);
            connectNewFast();
            connectOldFast();
            return;
        }// end function

        public function sendVideoInfo(param1:uint, param2:String) : Boolean
        {
            var iid:* = param1;
            var type:* = param2;
            if (fastType == OLD_FAST_TUDOU)
            {
                try
                {
                    oldSocket.send([S_LING, CMD_INFO, iid + "#" + type, S_FING].join(S_SPTA));
                    return true;
                }
                catch (e)
                {
                }
            }
            else
            {
                itemId = iid;
                itemType = type;
            }
            return false;
        }// end function

        public function report(param1:String, param2:uint, param3:uint, param4:uint) : void
        {
            var _loc_5:String = null;
            if (fastType == NEW_FAST_TUDOU)
            {
                _loc_5 = "http://" + NEW_HOST + ":" + SHAKE_NEW_PORT + "/playerprogress.html?";
                _loc_5 = _loc_5 + ("playid=" + param1);
                _loc_5 = _loc_5 + ("&itemid=" + itemId);
                _loc_5 = _loc_5 + ("&time=" + param2);
                _loc_5 = _loc_5 + ("&size=" + param3);
                _loc_5 = _loc_5 + ("&bitrate=" + param4);
                _loc_5 = _loc_5 + ("&type=" + itemType);
                _loc_5 = _loc_5 + ("&noCache=" + RandString.noCache);
                sendToURL(new URLRequest(_loc_5));
            }
            return;
        }// end function

        public function get canReport() : Boolean
        {
            return canReportFast;
        }// end function

        public function get isConnected() : Boolean
        {
            if (checkState != CHECKED)
            {
                return false;
            }
            if (fastType == OLD_FAST_TUDOU)
            {
                return oldSocket.connected;
            }
            if (fastType == NEW_FAST_TUDOU)
            {
                return true;
            }
            return false;
        }// end function

        public static function get inst() : FastTudouNew
        {
            if (instance == null)
            {
                lock = false;
                instance = new FastTudouNew;
                lock = true;
            }
            return instance;
        }// end function

    }
}
